﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Drawing.Design;


namespace Water.Web.Controls
{
    [ParseChildren(false)]
    //[ToolboxData("<tab title=\"new tab\"></tab>")]
    public class TabPage:WebControl
    {
        string title;
        [Browsable(true), GlobalizeDescription("Title"), Category("Appearance"), Bindable(true)]
        public string Title
        {
            get{return this.title;}
            set{ this.title= value;}
        }

        [Browsable(true), GlobalizeDescription("Title"), Category("Behavior")]
        public bool Selected
        {
            get
            {
                if (ViewState["Selected"] == null)
                    return false;
                return (bool)ViewState["Selected"];
            }
            set
            {
                ViewState["Selected"] = value;
            }
        }

        private string titleImageUrl = "";

        [Browsable(true), GlobalizeDescription("TitleImageUrl"), Category("Appearance"), Bindable(true), DefaultValue(""), Editor("System.Web.UI.Design.ImageUrlEditor", typeof(UITypeEditor)), UrlProperty]
        public virtual string TitleImageUrl
        {
            get { return titleImageUrl; }
            set { titleImageUrl = value; }
        }

        private string navigateUrl;

        [Editor("System.Web.UI.Design.UrlEditor", typeof(UITypeEditor)), Bindable(true), GlobalizeCategory("Navigation"), DefaultValue(""), UrlProperty, GlobalizeDescription("NavigateUrl")]
        public string NavigateUrl
        {
            get { return navigateUrl; }
            set { navigateUrl = value; }
        }

        private bool allowTargetInBlank;

        [Browsable(true), GlobalizeDescription("AllowTargetInBlank"), GlobalizeCategory("Navigation"), DefaultValue(false)]
        public bool AllowTargetInBlank
        {
            get { return allowTargetInBlank; }
            set { allowTargetInBlank = value; }
        }

        private TabNavigateType navigateType = TabNavigateType.Contain;

        [Browsable(true), GlobalizeDescription("NaviateType"), Category("Behavior"), DefaultValue(TabNavigateType.Contain)]
        public TabNavigateType NavigateType
        {
            get { return navigateType; }
            set { navigateType = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        private int index;

        internal int Index
        {
            get { return index; }
            set { index = value; }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.Parent.ClientID + "_tab" + index);
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Class,"tab_contain");
          
            if (this.Selected)
                writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "block");
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            if (TabNavigateType.Iframe == this.NavigateType)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "tab_iframe");
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.Parent.ClientID + "_iframe" + index);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, this.Parent.ClientID + "_iframe" + index);
                writer.AddAttribute("frameborder","0");
                writer.AddAttribute("scrolling", "Auto");
                writer.AddAttribute("marginwidth", "0");
                writer.AddAttribute("marginheight", "0");
                if (this.Selected)
                    writer.AddAttribute(HtmlTextWriterAttribute.Src,this.ResolveClientUrl(this.NavigateUrl));
                writer.RenderBeginTag(HtmlTextWriterTag.Iframe);
                writer.RenderEndTag();
            }

            if (this.Selected && TabNavigateType.Ajax == this.NavigateType)
            {
                string js = @"$j('#" + this.Parent.ClientID + "_a" + Index + "').click();";
                js = @"$j(function() {" + js + "});";
                this.Page.ClientScript.RegisterStartupScript(typeof(TabControl), this.UniqueID, js + "\n", true);
            }
        }
    }
    public enum TabNavigateType
    { 
        Contain,
        Iframe,
        Ajax
    }
}
